# Maintainer: Philippe Renon <philippe.renon@yahoo.fr>

_realname=rocksdb
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=6.7.3
pkgrel=1
pkgdesc="A library that provides an embeddable, persistent key-value store for fast storage (mingw-w64)"
arch=('any')
url="https://rocksdb.org/"
license=("Apache")
depends=("${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-intel-tbb"
         "${MINGW_PACKAGE_PREFIX}-lz4"
         "${MINGW_PACKAGE_PREFIX}-snappy"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc")
options=('staticlibs' 'strip')
source=(${_realname}-${pkgver}.tar.gz::"https://github.com/facebook/${_realname}/archive/v${pkgver}.tar.gz"
        "0004-fix-generating-and-install-import-lib.patch"
        "0005-fix-cast-between-incompatible-pointers.patch"
        "0006-cmake-build.patch"
        "0007-mingw-fix-build-flags.patch")
sha256sums=('c4d1397b58e4801b5fd7c3dd9175e6ae84541119cbebb739fe17d998f1829e81'
            '4ac7ef7039284f8bed32123e91405e6fbb4af91e70d059cbc2c973152f575960'
            'f0ce06542820a45eee74316f4b84a921f266032666a07e25284680ad1cc041da'
            'e3ca227ef5751fe558135720ee911c9747fb77a6d6b84a2f4001ccff7c224ee1'
            '14ed48fde07d6165316e467fa13f908f00a7efc3449c67a1256ff37cb4c88039')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}/0004-fix-generating-and-install-import-lib.patch"
  patch -p1 -i "${srcdir}/0005-fix-cast-between-incompatible-pointers.patch"
  patch -p1 -i "${srcdir}/0006-cmake-build.patch"
  patch -p1 -i "${srcdir}/0007-mingw-fix-build-flags.patch"
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}

  declare -a extra_config
  if check_option "debug" "n"; then
    # tests don't compile in Release mode
    extra_config+=("-DCMAKE_BUILD_TYPE=Release" "-DWITH_TESTS=OFF" "-DWITH_GFLAGS=OFF")
  else
    # not tested + needs gflags dependency
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${extra_config[@]}" \
    -DWITH_ZLIB=ON \
    -DWITH_BZ2=ON \
    -DWITH_LZ4=ON \
    -DWITH_ZSTD=ON \
    -DWITH_TBB=OFF \
    -DWITH_SNAPPY=ON \
    -DUSE_RTTI=ON \
    -DPORTABLE=ON \
    -DROCKSDB_INSTALL_ON_WINDOWS=ON \
    ../${_realname}-${pkgver}

  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install

  local WINPREFIX=$(cygpath -wm ${MINGW_PREFIX})
  # Fix paths
  sed -i "s|${WINPREFIX}|${MINGW_PREFIX}|g" \
    ${pkgdir}${MINGW_PREFIX}/lib/cmake/rocksdb/RocksDBTargets.cmake

  install -Dm644 "${srcdir}/${_realname}-${pkgver}"/LICENSE.Apache "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE
  install -Dm644 "${srcdir}/${_realname}-${pkgver}"/LICENSE.leveldb "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE.leveldb
  install -Dm644 "${srcdir}/${_realname}-${pkgver}"/COPYING "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
